<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/2/28
 * Time: 17:52
 */

namespace Resource\Service;
use Think\Model;

class DocumentService extends Model
{
    protected $trueTableName = 'wz_document';
    /**
     * @param $params
     * @return array|bool
     */
    public function addDocument($params) {
        $params['create_time'] = time();
        $params['update_time'] = time();
        $item_id = D('Resource/Document')->addDocument($params);
        if($item_id) {
            return ['id'=>$item_id];
        } else {
            return false;
        }
    }

    /**
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getDocumentInfo($params, $fields='') {
        $dataInfo = D('Resource/Document')->getDocumentInfo($params,$fields);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return array
     */
    public function searchDocumentList($params) {
        $page = 1;
        $page_size = 15;
        isset($params['page']) && $page = $params['page'];
        isset($params['page_size']) && $page_size = $params['page_size'];

        $data = [];
        isset($params['name']) && $data['name'] = ['like',"%{$params['name']}%"];
        $data['pid'] = $params['pid'];

        //查询项目总数
        $total = D('Resource/Document')->getDocumentCount($data);

        //计算总页数
        $total_page = ceil($total / $page_size);
        if($page < 1) {
            $page = 1;
        }
        if($page > $total_page) {
            $page = $total_page;
        }

        //查询项目列表
        $dataList = D('Resource/Document')->getDocumentList($data, $page, $page_size);

        $result = [];
        $result['page'] = $page;
        $result['page_size'] = $page_size;
        $result['total'] = $total;
        $result['total_page'] = $total_page;
        $result['dataList'] = $dataList;
        return $result;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateDocumentInfo($params) {
        return D('Resource/Document')->updateDocumentInfo($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function deleteDocument($params) {
        return D('Resource/Document')->deleteDocument($params);
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getAllDocumentList($params){
        $dataInfo = D('Resource/Document')->getAllDocumentList($params);
        return $dataInfo;
    }

    /**
     * @param $params
     * @return int
     */
    public function getDocumentCount($params){
        $count = D('Resource/Document')->getDocumentCount($params);
        return intval($count);
    }
}